package cn.spike.org.controller;

import cn.spike.basic.exception.BusinessException;
import cn.spike.basic.util.ExcelUtils;
import cn.spike.org.domain.ShopAuditLog;
import cn.spike.org.dto.ShopDto;
import cn.spike.org.service.ShopService;
import cn.spike.org.domain.Shop;
import cn.spike.org.query.ShopQuery;
import cn.spike.basic.util.AjaxResult;
import cn.spike.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@Api(value = "店铺的API",description = "处理店铺的操作")
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult save(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "删除单条数据")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个数据")
    public Shop loadOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @ApiOperation(value = "查询所有数据")
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @ApiOperation(value = "分页和高级查询")
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query) {
        return shopService.pageQuery(query);
    }

    /**
     * 接口：店铺入驻接口
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻接口")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

    /**
     * 接口：店铺审核通过接口
     * @param log  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过接口")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log){
        try {
            shopService.auditPass(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 接口：店铺审核驳回接口
     * @param log  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回接口")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log){
        try {
            shopService.auditReject(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 接口：导出店铺数据的接口
     * @param response  传递的实体
     * @return AjaxResult 响应给前端
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出店铺数据的接口")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"系统数据.xlsx",response);
    }

    /**
     * 接口：导入店铺数据的接口
     * @param file  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入店铺数据的接口")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        for (Shop shop:shops) {
            System.out.println(shop);
        }
    }

    /**
     * 点击激活连接之后的接口
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    public String activation(@PathVariable("id")Long id){
        try {
            shopService.activation(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "<h1>店铺激活成功</h1><h1><a href='http://127.0.0.1:8081/#/login'>点击前往登录</a></h1>";
    }

    @GetMapping("/echarts")
    public List<ShopDto> echarts(){
        return shopService.echarts();
    }


}
